package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R save(@RequestBody Category category, HttpSession session) {
        log.info("category:{}", category.toString());

        /*category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        Employee loginUser = (Employee) session.getAttribute("employee");
        category.setCreateUser(loginUser.getId());
        category.setUpdateUser(loginUser.getId());*/
        boolean save = categoryService.save(category);
        if (save) {
            return R.success("新增成功!");
        } else {
            return R.error("保存失败!");
        }
    }

    /**
     * 分页查询分类管理中菜系和套餐
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize) {
        //分页构造器
        Page<Category> pageInfo = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件,根据种类sort进行排序
        queryWrapper.orderByDesc(Category::getSort);

        //分页查询
        categoryService.page(pageInfo, null);
        return R.success(pageInfo);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    // 注释掉原有的代码，在delete方法中直接调用categoryService中我们自定义的remove方法
    @DeleteMapping
    public R<String> delete(Long id) {
        log.info("删除分类,id为:{}", id);
        //categoryService.removeById(id);
        categoryService.remove(id);
        return R.success("分类信息删除成功!");
    }

    /**
     * 修改分类信息
     *
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        log.info("修改分类信息: {}", category);
        categoryService.updateById(category);
        return R.success("修改分类信息成功！");
    }

    /**
     * 分类管理中 菜品分类查询
     *
     * @param category
     * @return
     */
    /*@GetMapping("/list")
    // select * from category where type=1
    // order by sort desc
    public R<List<Category>> list(Category category) {
        // 条件构造器
        LambdaQueryWrapper<Category> categoryQueryWrapper = new LambdaQueryWrapper<>();
        // 添加条件
        categoryQueryWrapper.eq(category.getType() != null, Category::getType, category.getType());
        //排序条件
        categoryQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getCreateTime);
        List<Category> list = categoryService.list(categoryQueryWrapper);
        return R.success(list);
    }*/

    /**
     * 分类管理中 菜品分类查询
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        // select * from category where type=1
        // order by sort desc
        // 条件构造器
        LambdaQueryWrapper<Category> categoryQueryWrapper = new LambdaQueryWrapper<>();
        // 添加条件
        categoryQueryWrapper.eq(category.getType() != null, Category::getType, category.getType());
        //排序条件
        categoryQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(categoryQueryWrapper);
        return R.success(list);
    }
}
